<?php
/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2019/10/22
 * Time: 1:44
 */

class ListNode{
    public $val = 0;
    public $next = null;
    function __construct($val)
    {
        $this->val = $val;
    }

}
class Solution_01
{
    /**
     * @param ListNode $l1
     * @param ListNode $l2
     * @return ListNode
     */

    function addTwoNumbers( $l1,  $l2){
        if(!$l1){
            $l1 = new ListNode(0);
        }
        if($l2){
            $tmp = $l1->val + $l2->val;
        }else{
            $tmp = $l1->val;
        }
        $l1->val = $tmp %10;
        if($l1->next){
            $l1->next->val = $l1->next->val + (int)($tmp/10);
        }else{
            $tmp = (int)($tmp/10);
            if($tmp){
             $l1->next = new ListNode($tmp);
            }
        }
        if(!$l1->next && !$l2->next){
            $tmp = $l1->val;
            $l1->val = $tmp%10;
            $tmp = (int)($tmp/10);
            if($tmp){
                $l1->next = new ListNode($tmp);
            }
        }
        if($l2->next){
            if(!$l1->next){
                $l1->next = new ListNode(0);
            }
            $l1->next = $this->addTwoNumbers( $l1->next, $l2->next);
        }else{
            if($l1->next){
                $l1->next = $this->addOneNumbers($l1->next);
            }
        }
        return $l1;
    }

    /**
     * addOneNumbers 遍历单个链表
     * @param ListNode $l1
     * @return ListNode
     */
    public function addOneNumbers(ListNode $l1){
        $tmp = $l1->val;
        $l1->val = $tmp%10;
        $tmp = (int)($tmp/10);
        if(!$l1->next){
            if($tmp){
                $l1->next = new ListNode($tmp);
            }
        }else{

            $l1->next->val =  $l1->next->val + $tmp;
            $l1->next = $this->addOneNumbers($l1->next);
        }
        return $l1;
    }
}

class Solution_02{
    public function addTwoNumbers($l1,$l2){
        $tmp = 0;
        $resListNode = $listNode= new ListNode(0);
        while ($l1 || $l2){
            $tmp = ($l1?$l1->val:0) +  ($l2?$l2->val:0) + $tmp;
            /*
             * 指针后移
             * */
            $resListNode->next = new ListNode($tmp%10);
            $resListNode = $resListNode->next;
            if($l1) $l1 = $l1->next;
            if($l2) $l2 = $l2->next;
            $tmp = (int)($tmp/10);
        }
        if($tmp){
            $resListNode->next = new ListNode($tmp);
        }
        return $listNode->next;
    }
}

class Solution_03{
    public function addTwoNumbers($l1,$l2){
        $tmp = 0;
        while ($l1 || $l2){
            $tmp = ($l1?$l1->val:0) +  ($l2?$l2->val:0) + $tmp;
            if(!isset($listNode)){
                $resListNode = $listNode= new ListNode($tmp%10);
            }else{
                $resListNode->next = new ListNode($tmp%10);
                $resListNode = $resListNode->next;
            }
            if($l1) $l1 = $l1->next;
            if($l2) $l2 = $l2->next;
            $tmp = floor($tmp/10);
        }
        if($tmp){
            $resListNode->next = new ListNode($tmp);
        }
        return $listNode;
    }


}
$solution = new Solution_03();

$l1 = new ListNode(2);
$l1->next = new ListNode(4);
$l1->next->next = new ListNode(3);

$l2 = new ListNode(5);
$l2->next = new ListNode(6);
$l2->next->next = new ListNode(9);

print_r($solution->addTwoNumbers($l2,$l1));